package com.hongyun.tms.service.impl;

import com.hongyun.tms.common.dto.CheckDTO;
import com.hongyun.tms.common.utils.StringUtil;
import com.hongyun.tms.mapper.CommonMapper;
import com.hongyun.tms.service.CommonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private CommonMapper commonMapper;

    /**
     * 验证简单的重复字段
     * @param tableName
     * @param conditionColNm
     * @param conditionColVal
     * @param id
     * @param errorKeyWord
     * @param otherCondition
     * @return
     */
    @Override
    public boolean isRepeate(String tableName, String conditionColNm, String conditionColVal, String id, String errorKeyWord, List<CheckDTO> keyValues, String... otherCondition) {
        final String methodName = "isRepeate";

        // 自定义msg
        String selfMsg = (otherCondition != null && otherCondition.length == 1) ? otherCondition[0] : null;

        // 是否显示msg
        String showMsgFlag = (otherCondition != null && otherCondition.length == 2) ? otherCondition[1] : null;

//        int isDelFlagExists = commonMapper.isDelFlagExists(tableName);
        int isDelFlagExists = 0;

        int count = commonMapper.checkRepeat(tableName, conditionColNm, conditionColVal, id, keyValues, isDelFlagExists);

        if(count > 0) {

            if ("0".equals(showMsgFlag)) {
                return true;
            }

            String errorMsg = selfMsg;

            if (StringUtil.isEmpty(selfMsg)) {
//                errorMsg = messageUtils.getMessage("wtos.message.common.error.repeate", new Object[] {errorKeyWord});
                errorMsg = errorKeyWord + "重复";
            }

            throw new RuntimeException(errorMsg);
        }
        return false;
    }
}
